package com.ld.security.server.admin.modules.msg.api;


import com.ld.security.auth.shiro.util.LoginUtil;
import com.ld.security.common.mybatis.base.po.EntityPo;
import com.ld.security.common.mybatis.base.po.IdsPo;
import com.ld.security.common.mybatis.base.po.PagePo;
import com.ld.security.common.mybatis.base.vo.DataVo;
import com.ld.security.common.mybatis.base.vo.ListVo;
import com.ld.security.common.mybatis.base.vo.PageVo;

import com.ld.security.common.tomcat.anno.AjaxLogKey;
import com.ld.security.server.admin.modules.msg.entity.MsgGroupNewsEntity;
import com.ld.security.server.admin.modules.msg.service.IMsgGroupNewsService;
import com.ld.security.server.admin.modules.msg.service.IMsgGroupNewsUserRelService;
import com.ld.security.server.admin.modules.msg.service.IMsgGroupService;
import com.ld.security.server.admin.modules.msg.vo.MyGroupMsgVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags="消息主页管理")
@RequestMapping("/api/msgAdmin")
public class MagAdminApi {

    @Autowired
    IMsgGroupNewsService iMsgGroupNewsService;

    @Autowired
    IMsgGroupService iMsgGroupService;

    @Autowired
    IMsgGroupNewsUserRelService iMsgGroupNewsUserRelService;

    @AjaxLogKey
    @RequestMapping(value = "getMyGroupMsg",method = RequestMethod.POST)
    @ApiOperation("获得指定群组消息")
    public DataVo getMyGroupMsgVo(@RequestBody PagePo<MyGroupMsgVo> po){
        //强制时间逆序
        po.setOrderBy("b.crt_time");
        po.setAsc(false);
        String userId = LoginUtil.getUserId();
        po.getQuery().setUserId(userId);
        PageVo vo =  iMsgGroupService.getMyGroupMsgVo(po);
        return DataVo.of(vo);
    }

    @AjaxLogKey
    @RequestMapping(value = "getMyGroupNewsTotal",method = RequestMethod.POST)
    @ApiOperation("获取我的群组列表和没读消息总数")
    public DataVo getMyGroupNewsTotal(){
        String userId = LoginUtil.getUserId();
        List list =  iMsgGroupService.getMyGroupNewsTotal(userId);
        return DataVo.of(ListVo.of(list));
    }


    @AjaxLogKey
    @RequestMapping(value = "readMsg",method = RequestMethod.POST)
    @ApiOperation("消息已读")
    public DataVo readMsg(@RequestBody IdsPo po){
       int total =  iMsgGroupNewsUserRelService.readMsg(po.getIds());
       return DataVo.ofMsg("已读"+total+"条信息");
    }

    @AjaxLogKey
    @RequestMapping(value = "saveMsg",method = RequestMethod.POST)
    @ApiOperation("添加一个消息")
    public DataVo saveMsg(@RequestBody EntityPo<MsgGroupNewsEntity> po){

        iMsgGroupNewsService.save(po.getEntity());
        return DataVo.of(po.getEntity());
    }
}
